$(function () {
    /* 为根节点绑定事件处理函数 */
    $(".dirbox-body .treeNode-info").click(showSubDir);
});

/*
 * 单击模态框的文件夹节点时绑定的事件处理函数
 */
function showSubDir() {
    var dirID = $(this).attr("data-folder-id");
    /* 把操作路径的文件夹ID设为当前点击文件夹的ID */
    $("#dirbox_path").attr("data-folder-id", dirID);

    /* 判断节点是否存在，如果不存在则生成，存在则显示 */
    if (getFolderNode(dirID) == null) {
        createFolderNode(dirID, false);
    } else {
        toggleExistSubDir($(this));
    }
}

/*
 * 显示/隐藏已经生成过的子文件夹并更新操作路径
 */
function toggleExistSubDir(dir) {
    /* 更新文件夹图标 */
    var icon = dir.find(".glyphicon");
    if (icon.hasClass("glyphicon-folder-open")) {
        icon.removeClass("glyphicon-folder-open");
        icon.addClass("glyphicon-folder-close");
    } else {
        icon.removeClass("glyphicon-folder-close");
        icon.addClass("glyphicon-folder-open");
    }
    /* 显示或隐藏子文件夹 */
    dir.next().slideToggle("fast");
    /* 更新操作路径 */
    var path = dir.find(".treeNode-info-name").text();
    if (path != "我的网盘") {
        var parentNode = dir;
        do {
            parentNode = parentNode.parent().parent().prev();
            var parent = parentNode.find(".treeNode-info-name").text();
            path = parent + " \\ " + path;
        } while (parent != "我的网盘");
    }
    $("#dirbox_path").text(path);
}
/*
 * 供file-system.js中的createFolderNode()函数和创建文件夹时调用
 * 生成主页面文件夹节点的同时会生成模态框文件夹节点
 */
function createDirNode(folderID, folders, show) {

    var dir = $('div[data-folder-id="' + folderID + '"]');
    var subDirs = dir.next();
    var paddingParam = parseInt(dir.css("padding-left"), 10) + 20 + "px";
    for (var i = 0; i < folders.length; i++) {
        var id = folders[i].folderId;
        var localName = folders[i].folderName;
        var subNode = $("<li></li>");
        subNode.append('<div class="treeNode-info"><span class="glyphicon glyphicon-folder-close"></span><span class="treeNode-info-name">' + localName + '</span></div>');
        subNode.append('<ul style="display: none;"></ul>');
        subNode.find(".treeNode-info").css("padding-left", paddingParam);
        subNode.find(".treeNode-info").attr("data-folder-id", id);
        subNode.find(".treeNode-info").click(showSubDir);
        subNode.appendTo(subDirs);
    }
    /* 因为ajax的原因，判断是否显示的逻辑应该在本函数内实现 */
    if (show) {
        toggleExistSubDir(dir);
    }
}

/*课程部分*/
function showCourseSubDir() {
    var dirID = $(this).attr("data-courseInfo-folder-id");
    /* 把操作路径的文件夹ID设为当前点击文件夹的ID */
    $("#dirbox_path").attr("data-courseInfo-folder-id", dirID);

    /* 判断节点是否存在，如果不存在则生成，存在则显示 */
    if (getCourseFolderNode(dirID) == null) {
        createCourseFolderNode(dirID, false);
    } else {
        toggleCourseExistSubDir($(this));
    }
}
function toggleCourseExistSubDir(dir) {
    /* 更新文件夹图标 */
    var icon = dir.find(".glyphicon");
    if (icon.hasClass("glyphicon-folder-open")) {
        icon.removeClass("glyphicon-folder-open");
        icon.addClass("glyphicon-folder-close");
    } else {
        icon.removeClass("glyphicon-folder-close");
        icon.addClass("glyphicon-folder-open");
    }
    /* 显示或隐藏子文件夹 */
    dir.next().slideToggle("fast");
    /* 更新操作路径 */
    var path = dir.find(".treeNode-info-name").text();
    var courseId=sessionStorage.getItem("currentCourseId");

    console.log("当前课程id:"+courseId);

    var CourseName=$('li[course-id="'+courseId+'"]').children(":first").find(".treeNode-info-name").text();

    console.log("课程名："+CourseName);

    if (path != CourseName) {
        var parentNode = dir;
        do {
            parentNode = parentNode.parent().parent().prev();
            var parent = parentNode.find(".treeNode-info-name").text();
            path = parent + " \\ " + path;
        } while (parent != CourseName);
    }
    $("#dirbox_path").text(path);
}
function createCourseDirNode(folderID, folders, show) {

    var dir = $('div[data-courseInfo-folder-id="' + folderID + '"]');
    var subDirs = dir.next();
    var paddingParam = parseInt(dir.css("padding-left"), 10) + 20 + "px";
    for (var i = 0; i < folders.length; i++) {
        var id = folders[i].folderId;
        var localName = folders[i].folderName;
        var subNode = $("<li></li>");
        subNode.append('<div class="treeNode-info"><span class="glyphicon glyphicon-folder-close"></span><span class="treeNode-info-name">' + localName + '</span></div>');
        subNode.append('<ul style="display: none;"></ul>');
        subNode.find(".treeNode-info").css("padding-left", paddingParam);
        subNode.find(".treeNode-info").attr("data-courseInfo-folder-id", id);
        subNode.find(".treeNode-info").click(showCourseSubDir);
        subNode.appendTo(subDirs);
    }
    /* 因为ajax的原因，判断是否显示的逻辑应该在本函数内实现 */
    if (show) {
        toggleCourseExistSubDir(dir);
    }
}

function createCourseListInPath(course) {
    //添加到模态框的ul中
    var courseId=course.courseId;
    var folderID = course.folderId;
    var courseName = course.courseName;
    //生成li
    var courseList=$('<li course-id="'+courseId+'"></li>');
    //添加div
    var courseDiv=$('<div class="treeNode-info" style="padding-left: 5px;" data-courseInfo-folder-id="'+folderID+'"></div>')
    courseDiv.append('<span class="glyphicon glyphicon-folder-open"></span>');
    courseDiv.append('<span class="treeNode-info-name">'+courseName+'</span>');
    courseList.append(courseDiv);
    //添加ul
    courseList.append('<ul></ul>');
    //添加到modal中
    courseList.appendTo($("#modal-ul"));
}




/*
 * 模态框取消按钮和x按钮绑定的事件处理函数
 */
function removeAddedMission() {
    /* 移除所有可操作按钮绑定的事件 */
    $("#modal_btn_submit").off("click");
    $("#modal_btn_cancel").off("click");
    $("#modal_btn_close").off("click");
}

